package com.sparrow.support;

import java.sql.Connection;
import java.sql.ResultSet;
import java.util.List;
import com.sparrow.orm.SQLHelper;

public interface DaoSupport<T> {

	/**
	 * 新建(支持分表)
	 * 
	 * @author zhanglizhi
	 * @date 2012-4-7
	 * @version 1.0
	 * @param model
	 * @return
	 * @throws Exception
	 */
	int insert(T model) throws Exception;

	/**
	 * 新建用于事务
	 * 
	 * 支持分表
	 * 
	 * @author zhanglizhi
	 * @date 2012-4-7
	 * @version 1.0
	 * @param model
	 * @param connection
	 * @return
	 * @throws Exception
	 */
	int insert(T model, Connection connection) throws Exception;

	/**
	 * 更新
	 * 
	 * 支持分表
	 * 
	 * @author zhanglizhi
	 * @date 2012-4-7
	 * @version 1.0
	 * @param model
	 * @throws Exception
	 */
	int update(T model) throws Exception;

	/**
	 * 更新 用于事务
	 * 
	 * 支持事务
	 * 
	 * @author zhanglizhi
	 * @date 2012-4-7
	 * @version 1.0
	 * @param model
	 * @param connection
	 * @throws Exception
	 */
	int update(T model, Connection connection) throws Exception;

	/**
	 * 更新 指定字段
	 * 
	 * 
	 * 操作
	 * 
	 * @author zhanglizhi
	 * @date 2012-4-7
	 * @version 1.0
	 * @param setClause
	 * @param whereClause
	 * @throws Exception
	 */
	int update(SQLHelper helper) throws Exception;

	/**
	 * 更新指定字段 用于事务
	 * 
	 * @author zhanglizhi
	 * @date 2012-4-7
	 * @version 1.0
	 * @param setClause
	 * @param whereClause
	 * @param connection
	 * @throws Exception
	 */
	int update(SQLHelper helper, Connection connection) throws Exception;

	/**
	 * 删除指定记录
	 * 
	 * @author zhanglizhi
	 * @date 2012-4-7
	 * @version 1.0
	 * @param uuid
	 * @throws Exception
	 */
	int delete(Object id) throws Exception;

	/**
	 * 删除某条记录，用于事务
	 * 
	 * @author zhanglizhi
	 * @date 2012-4-7
	 * @version 1.0
	 * @param id
	 * @param connection
	 * @throws Exception
	 */
	int delete(Object id, Connection connection) throws Exception;

	/**
	 * 删除N条记录根据条件
	 * 
	 * @author zhanglizhi
	 * @date 2012-4-7
	 * @version 1.0
	 * @param whereClause
	 * @throws Exception
	 */
	int delete(SQLHelper helper) throws Exception;

	/**
	 * 根据条件删除（用于事务)
	 * 
	 * @author zhanglizhi
	 * @date 2012-4-7
	 * @version 1.0
	 * @param whereClause
	 * @param connection
	 * @throws Exception
	 */
	int delete(SQLHelper helper, Connection connection) throws Exception;

	/**
	 * @author zhanglizhi
	 * @date 2012-12-17
	 * @version 1.0
	 * @param fields
	 * @param whereClause
	 * @param orderClause
	 * @param groupClause
	 * @param pageSize
	 * @param currentPageIndex
	 * @param intRecordCount
	 * @return
	 */
	ResultSet select(SQLHelper helper);

	T getEntity(Object id) throws Exception;

	T getEntity(Object key, int uniqueKeyIndex) throws Exception;

	T getEntity(SQLHelper helper) throws Exception;

	List<T> getList(SQLHelper helper) throws Exception;

	/**
	 * 用来判断重复添加
	 * 
	 * 对于unique字段
	 * 
	 * @author zhanglizhi
	 * @date 2013-4-16
	 * @version 1.0
	 * @param key
	 * @param uniqueKeyIndex
	 * @return
	 * @throws Exception
	 */
	int getCount(Object key, int uniqueKeyIndex) throws Exception;

	int getCount(Object key) throws Exception;

	int getCount(SQLHelper helper);

	int getAggregate(String aggregate) throws Exception;

	String getFieldValue(String fieldName, String value) throws Exception;

	String getFieldValue(String fieldName, Object key, int uniqueIndex)
			throws Exception;
	
	void release(ResultSet rs);
}